import {Text, View} from "@tarojs/components";
import DynamicForm, {FormConfig} from "@/components/DynamicForm";
import LocalData from "@/common/local_data";
import {Button, Cell} from "@antmjs/vantui";
import Utils from "@/utils/common";
import LogUtils from "@/utils/log";
import {idCardVerification} from "@/common/apis/home_datasource";
import LocalUtils from "@/utils/local_utils";
import EventUtils from "@/utils/event_utils";
import {useRef} from "react";
import RouthPath from "@/common/RouthPath";

definePageConfig({
  navigationBarTitleText: '实名认证'
})
export default function Real_info_verify() {
  // 表单配置
  const verifyFormConfigs: FormConfig[] = [
    {
      type: 'input',
      inputType: LocalData.numInputType,
      name: 'realName', // Logical name for recipient name
      label: '真实姓名',       // Label text (without *)
      placeholder: '请输入真实姓名',
      required: true,
      layout: 'horizontal',
      rules: []
    },
    {
      type: 'input',
      name: 'idNo', // Logical name for recipient name
      label: '身份证号',       // Label text (without *)
      placeholder: '请输入身份证号码',
      required: true,
      layout: 'horizontal',
      rules: [
        {
          rule: /^[1-9]\d{5}(18|19|(2\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
          message: '输入正确的身份证',
        }
      ]
    },
    {
      type: 'textarea',
      name: 'company', // Logical name for recipient name
      label: '学校/组织',       // Label text (without *)
      maxLength: 100,
      disabled: true,
      minHeight: '10px',
      placeholder: '学校或组织',
      renderRight: () => {
        return <Button type='primary' size='small' style={{width: "130px"}} onClick={() => {
          Utils.navigateAndListenToEvent(RouthPath.universityList, EventUtils.OK, (data) => {
            if (data) {
              childRef.current?.setFieldValue("company", data)
            }
          })
        }}
        >学校/组织</Button>;
      },
      required: true,
      layout: 'horizontal',
      rules: []
    },
    {
      type: 'render',
      name: 'tips', // Logical name for recipient name
      label: '',       // Label text (without *)
      renderReactNode: () => {
        return <View>
          <Cell renderTitle={<Text style={{color: 'red'}}>请认真填写，避免填写错误</Text>}/>
        </View>
      },
    },
  ];
  const childRef = useRef(null); // 仍然使用 useRef
  // 提交回调
  const handleSubmit = (values) => {
    idCardVerification(values).then(res => {
      if (res) {
        Utils.showOkToast('认证成功')
        LocalUtils.setIdCard("1");
        EventUtils.trigger(EventUtils.UserInfo, true)
        Utils.navigateBack();
      }
    })
    // 处理表单提交逻辑
  };

  // 错误回调
  const handleError = (errors) => {
    LogUtils.info('errors:', errors);
  };

  return (
    <View>
      <DynamicForm
        ref={childRef}
        formConfigs={verifyFormConfigs}
        onSubmit={handleSubmit}
        onError={handleError}
        submitText='提交'
      />
    </View>
  )
    ;
}


